package cn.mw.cmdb.service;

import cn.mw.cmdb.entity.InstanceInfo;
import cn.mw.cmdb.enums.MonitoringServerType;
import cn.mw.cmdb.param.QueryMonitorParam;
import cn.mw.cmdb.processor.MonitorParamContext;
import cn.mw.cmdb.processor.MonitorProcessor;
import cn.mw.cmdb.service.impl.CmdbServiceManage;
import cn.mw.cmdb.service.impl.InstanceServiceImpl;
import cn.mw.components.mongodb.mapper.BaseMapper;
import cn.mw.main.api.common.ZabbixDubboService;
import cn.mw.microMonitorCommon.entity.common.MwTPServerTable;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.google.common.base.Strings;

import java.util.Arrays;

public class MwMonitorProcessorConvert {
    private CmdbServiceManage cmdbServiceManage;
    private ZabbixDubboService zabbixDubboService;

    public MwMonitorProcessorConvert(CmdbServiceManage cmdbServiceManage, ZabbixDubboService zabbixDubboService) {
        this.cmdbServiceManage = cmdbServiceManage;
        this.zabbixDubboService = zabbixDubboService;
    }

    public MwMonitorProcessorConvert() {
    }

    public MonitorProcessor getMonitorProcessor(String instanceId) {
        MonitorProcessor processor = null;
        if (!Strings.isNullOrEmpty(instanceId)) {
            InstanceServiceImpl modelService = (InstanceServiceImpl) cmdbServiceManage.getModelService(InstanceServiceImpl.ID);
            InstanceInfo instanceParam = new InstanceInfo();
            instanceParam.setId(instanceId);
            InstanceInfo instanceInfo = modelService.getInstanceInfoById(instanceId);
            if (instanceInfo != null && !Strings.isNullOrEmpty(instanceInfo.getMonitorType()) && !Strings.isNullOrEmpty(instanceInfo.getMonitorTypeId())) {
                MwTPServerTable mwTPServerTable = new MwTPServerTable();
                mwTPServerTable.setMonitoringServerType(instanceInfo.getMonitorType());
                processor = MonitorProcessor.build(mwTPServerTable, Arrays.asList(instanceInfo));
            }
        }
        return processor;
    }

    public MonitorProcessor getMonitorProcessor(QueryMonitorParam param) {
        MonitorProcessor processor = null;
        if (param != null) {
            if (!Strings.isNullOrEmpty(param.getMonitorType()) && CollectionUtils.isNotEmpty(param.getHostIds())) {
                MwTPServerTable mwTPServerTable = new MwTPServerTable();
                mwTPServerTable.setMonitoringServerType(param.getMonitorType());
                processor = MonitorProcessor.build(mwTPServerTable, null);
            }
        }
        return processor;
    }

    public MonitorProcessor getMonitorProcessor(MonitoringServerType type) {
        MonitorProcessor processor = null;
        if (type !=null) {
            MwTPServerTable mwTPServerTable = new MwTPServerTable();
            mwTPServerTable.setMonitoringServerType(type.name());
            processor = MonitorProcessor.build(mwTPServerTable, null);
        }
        return processor;
    }

    public MonitorParamContext getContextParam(Object param, BaseMapper baseMapper) {
        MonitorParamContext context = MonitorParamContext.build();
        context.setZabbixDubboService(zabbixDubboService);
        if (param != null) {
            context.setParam(param);
        }
        if (baseMapper != null) {
            context.setBaseMapper(baseMapper);
        }
        return context;
    }

    public MonitorParamContext getContextParam() {
        MonitorParamContext context = MonitorParamContext.build();
        context.setZabbixDubboService(zabbixDubboService);
        return context;
    }
}
